package logic;

import java.util.Collections;
import java.util.Comparator;
import java.util.Vector;

public class TexasHoldemTable {
	private Vector<Card> slot;
	private Vector<Hand> handCombinations;
	private float pot = 0.0f;

	public void setSlot(String cards) {
		slot = new Vector<Card>();
		
		for(String c : cards.split(" ")) {
			slot.add(new Card(c));
		}
	}

	public Hand bestFive(Vector<Card> cards) {
		slot.addAll(cards);
		fillHandCombinations();
		slot.removeAll(cards);
		
		Collections.sort(handCombinations,new HandComparator());
		
		return handCombinations.lastElement();
	}

	private void fillHandCombinations() {
		Hand hand;
		handCombinations = new Vector<Hand>();
		
		for(int i=0;i<6;i++) {
			for(int k=i+1;k<7;k++) {
				hand = new Hand();
				for(int j=0;j<7;j++) {
					if((j != k) && (j != i)) {
						hand.addCard(slot.get(j));
					}
				}
				hand.prepare();
				handCombinations.add(hand);
			}
		}		
	}

	public String showSlot() {
		String slotView = "";
		
		for(Card c : slot) {
			slotView += c.getName()+" ";
		}
		
		if(slotView.length() > 0) {
			return slotView.substring(0, slotView.length()-1);
		}
		
		return slotView;
	}

	public void addToPot(float f) {
		this.pot  += f;
	}

	public float showPot() {
		return pot;
	}

	public void cleanSlot() {
		if(slot == null) {
			slot = new Vector<Card>();
		}else {
			slot.removeAllElements();
		}
	}

	public void addCardToSlot(Card card) {
		slot.add(card);
	}

	public void clearPot() {
		pot = 0.0f;
	}
	
}

class HandComparator implements Comparator<Hand> {

	@Override
	public int compare(Hand h0, Hand h1) {
		return (Tools.bestGame(h0,h1) == h0)?1:-1;
	}
	
}